package sim.gui;

import java.awt.* ;
import java.util.* ;
import java.awt.geom.* ;

import sim.core.*;
import sim.util.*;

@SuppressWarnings("serial")
public class Panel extends javax.swing.JPanel {
	
	Panel() {
		super.setDoubleBuffered(true) ;
	}
	
	private XY mouse = new XY(-1, -1);
    public static double f = 0.925 ;
    public static final double S = 1000.0 ;
    private static double w, h, l, tx, ty, s, _s ;
	Collection<Displayable> items = new ArrayList<Displayable>() ;
    
    

    public void paintComponent( Graphics g ) {
        super.paintComponent( g ) ;
        
        Graphics2D gu = (Graphics2D) g ;
        gu.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ) ;
        
        Graphics2D gs = (Graphics2D)gu.create() ;
        
        XY p = mouse ;
        gu.setColor( Color.gray ) ;
        gu.setStroke( new BasicStroke( 1.0f ) ) ;
        gu.drawLine( p.X() - 4, p.Y() - 4, p.X() + 4, p.Y() + 4 ) ;
        gu.drawLine( p.X() - 4, p.Y() + 4, p.X() + 4, p.Y() - 4 ) ;
        
        
        w = getWidth() ;
        h = getHeight() ;
        l = Math.min( w, h ) * f ;
        
        tx = ( w - l ) / 2 ;
        ty = ( h - l ) / 2 ;
                
        s = l / S ;
        _s = S / l ;
        
        AffineTransform t = new AffineTransform() ;
        t.translate( tx, ty ) ;
        t.scale( s, s ) ;
        gs.setTransform( t ) ;
        
        gs.setColor( Color.lightGray ) ;
        gs.setStroke( new BasicStroke( 0.1f ) ) ;
        gs.draw( new Rectangle2D.Double( 0, 0, S, S ) ) ;
        
        for( Displayable i : new ArrayList<Displayable>(items) )
        	i.display( gu, gs ) ;
    }
    
    public void setMousePosition( int x, int y ) {
    	mouse = new XY( x, y ) ;
    }
    
    public XY mouse() {
        double x = mouse.x;
        double y = mouse.y;
        double xx = (x - tx) * _s ;
        double yy = (y - ty) * _s;
        return new XY(xx,yy) ;
    }   
}
